//
//  JHApiProvider.swift
//  test-ios
//
//  Created by wyz5204 on 2023/3/27.
//

import Foundation
import Moya

enum JH{
    case joke(page:Int)
}

extension JH:TargetType{
    
    var baseURL: URL {
        return URL(string : "http://v.juhe.cn/")!
    }
    
    var path: String {
        switch(self){
        case .joke :
            return "joke/content/list.php"
        }
        return ""
    }
    
    var method: Moya.Method {
        switch(self){
        case .joke : return .get
        }
        return .post
    }
    
    var task: Task {
        var urlParameters = [String: Any]()
        var bodyParameters = [String: Any]()
        switch(self){
        case .joke(let page) :
            urlParameters["key"] = "c68a247a2a65c9c96c91e1c100c61cc3"
            urlParameters["page"] = page
            urlParameters["pagesize"] = 20
            urlParameters["sort"] = "desc"
            urlParameters["time"] = Int(round(Date.init().timeIntervalSince1970)).description
            return .requestParameters(parameters: urlParameters, encoding: URLEncoding.queryString)
        }
        return .requestParameters(parameters:bodyParameters, encoding: JSONEncoding.default)
    }
    
    var headers: [String : String]? {
        var headers: [String: String] = [:]
        switch self {
        case .joke:
            headers["Content-type"] = "application/x-www-form-urlencoded"
        default:
            headers["Content-type"] = "application/json;charset=utf-8"
        }
        return headers
    }
    
}



